package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsZone;
import net.realsee.domain.wcs.data.bo.WcsUnitCharactBo;
import net.realsee.domain.wcs.data.bo.WcsZoneBo;
import net.realsee.domain.wcs.data.vo.WcsZoneVo;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.mapper.wcs.data.WcsZoneMapper;
import net.realsee.service.wcs.data.IWcsZoneService;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 区域Service业务层处理
 *
 * @author realsee
 * @date 2023-08-21
 */
@RequiredArgsConstructor
@Service
public class WcsZoneServiceImpl extends ServiceImpl<WcsZoneMapper, WcsZone> implements IWcsZoneService {

    private final WcsZoneMapper baseMapper;

    /**
     * 查询区域
     */
    @Override
    public WcsZoneVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询区域列表
     */
    @Override
    public TableDataInfo<WcsZoneVo> selectVoPage(WcsZoneBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsZone> lqw = buildVoQueryWrapper(bo);
        Page<WcsZoneVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询区域列表
     */
    @Override
    public List<WcsZoneVo> selectVoList(WcsZoneBo bo) {
        LambdaQueryWrapper<WcsZone> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsZone> buildVoQueryWrapper(WcsZoneBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsZone> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getZone()), WcsZone::getZone, bo.getZone());
        lqw.eq(StringUtils.isNotBlank(bo.getName()), WcsZone::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouse()), WcsZone::getWarehouse, bo.getWarehouse());
        lqw.eq(StringUtils.isNotBlank(bo.getStorageType()), WcsZone::getStorageType, bo.getStorageType());
        return lqw;
    }

    /**
     * 新增区域
     */
    @Override
    public Boolean insertByBo(WcsZoneBo bo) {
        WcsZone add = BeanUtil.toBean(bo, WcsZone.class);
        return this.save(add);
    }

    /**
     * 修改区域
     */
    @Override
    public Boolean updateByBo(WcsZoneBo bo) {
        WcsZone update = BeanUtil.toBean(bo, WcsZone.class);
        return this.updateById(update);
    }

    /**
     * 批量删除区域
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    /**
     * 查询库位所在区域的传感器距离
     * @param location
     * @return
     */
    @Override
    public List<WcsUnitCharactBo> getZoneSensorDistanceList(String location) {
        return baseMapper.getZoneSensorDistanceList(location);
    }
}
